package com.example.hystrix;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;


@SpringBootApplication
@Configuration
@EnableHystrix
@EnableHystrixDashboard
//@EnableTurbine
@RestController
public class SampleHystrixApp {

	@Autowired
	Service service;
	
	@RequestMapping("/test")
	public String testCommand() {
		return service.testCommand();
	}
	@RequestMapping("/test2")
	public String testCircuit() {
		return service.testCircuit();
	}

	@Bean
	Service service() {
		return new Service();
	}

	public static void main(String[] args) {
		SpringApplication.run(SampleHystrixApp.class, args);
	}
}
class Service {

	@HystrixCommand(groupKey="ExampleGroup",commandKey="ExampleCommand",threadPoolKey="ExampleThreadPool")
	public String testCommand() {
		return "testCommand";
	}
	@HystrixCommand(groupKey="ExampleGroup",commandKey="ExampleCommand",threadPoolKey="ExampleThreadPool")
	public String testCircuit() {
		int i=1/0;//mock error
		return "testCircuit";
	}
}